package com.ruoyi.carrepair.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.carrepair.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.carrepair.service.IQxWxdNrService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修单管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/carrepair/wxdnr")
public class QxWxdNrController extends BaseController
{
    @Autowired
    private IQxWxdNrService qxWxdNrService;

    /**
     * 查询维修单管理列表
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:list')")
    @GetMapping("/list")
    public TableDataInfo list(QxWxdNr qxWxdNr)
    {
        if(qxWxdNr.getIsjiesuan() == 3){
            qxWxdNr.setIsjiesuan(null);
        }
        startPage();
        List<QxWxdNr> list = qxWxdNrService.selectQxWxdNrList(qxWxdNr);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:list')")
    @GetMapping("/getSum")
    public QxWxdNrSummary getSum(QxWxdNr qxWxdNr)
    {
        if(qxWxdNr.getIsjiesuan() == 3){
            qxWxdNr.setIsjiesuan(null);
        }
        QxWxdNrSummary qxWxdNrSummary = qxWxdNrService.selectSumQxWxdNr(qxWxdNr);
        return qxWxdNrSummary;
    }

    /**
     * 导出维修单管理列表
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:export')")
    @Log(title = "维修单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QxWxdNr qxWxdNr)
    {
        List<QxWxdNr> list = qxWxdNrService.selectQxWxdNrList(qxWxdNr);
        ExcelUtil<QxWxdNr> util = new ExcelUtil<QxWxdNr>(QxWxdNr.class);
        util.exportExcel(response, list, "维修单管理数据");
    }

    /**
     * 获取维修单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(qxWxdNrService.selectQxWxdNrById(id));
    }

    /**
     * 新增维修单管理
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:add')")
    @Log(title = "维修单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxdParams wxdParams)
    {
        QxWxdNr qxWxdNr = JSON.parseObject(wxdParams.getNr(),QxWxdNr.class);
        List<QxMxPeijian> listPeijian = JSON.parseArray(wxdParams.listPeijian, QxMxPeijian.class);
        List<QxMxWeixiu> listWeixiu = JSON.parseArray(wxdParams.listWeixiu, QxMxWeixiu.class);

        String riqi = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        int num = qxWxdNrService.selectNums(riqi);
        String danhao = this.createOrderNo(num);

        Map<String,String> result = new HashMap<>();
        result.put("returnId",qxWxdNrService.insertQxWxdNr(qxWxdNr,listPeijian,listWeixiu,danhao));
        return success(result);
    }

    /**
     * 修改维修单管理
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:edit')")
    @Log(title = "维修单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxdParams wxdParams)
    {
        QxWxdNr qxWxdNr = JSON.parseObject(wxdParams.getNr(),QxWxdNr.class);
        List<QxMxPeijian> listPeijian = JSON.parseArray(wxdParams.listPeijian, QxMxPeijian.class);
        List<QxMxWeixiu> listWeixiu = JSON.parseArray(wxdParams.listWeixiu, QxMxWeixiu.class);
        //System.out.println(qxWxdNr);
        //System.out.println(listPeijian);
        //System.out.println(listWeixiu);
        Map<String,String> result = new HashMap<>() ;
        result.put("returnId",qxWxdNrService.updateQxWxdNr(qxWxdNr,listPeijian,listWeixiu));
        return success(result);

    }

    private String createOrderNo(int num){
        String riqiStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String numStr="";
        if(num<10){
            numStr = riqiStr + numStr + "00"+num;
        }else if(num>=10 && num<100){
            numStr = riqiStr +  numStr + "0" + num;
        }else{
            numStr = riqiStr +  numStr + num;
        }
        return numStr;
    }

    /**
     * 删除维修单管理
     */
    @PreAuthorize("@ss.hasPermi('carrepair:wxdnr:remove')")
    @Log(title = "维修单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(qxWxdNrService.deleteQxWxdNrByIds(ids));
    }


    @GetMapping("/audit/{ids}")
    public AjaxResult audit(@PathVariable String[] ids)
    {
        return toAjax(qxWxdNrService.auditById(ids));
    }
}
